# 非递归算法：
def binary_search(sort_array, target):
    if len(sort_array) == 0:
        return -1
    beg = 0
    end = len(sort_array)
    while beg <= end:
        mid = beg + (end - beg) // 2
        if sort_array[mid] == target:
            return mid
        elif sort_array[mid] > target:
            end = mid - 1
        elif sort_array[mid] < target:
            beg = mid + 1
    return -1


# 递归算法：
def binary_search_two(sorted_array,low,high, target):
    if low>high:
        return -1
    mid = (low + high) // 2
    if sorted_array[mid] == target:
        return mid
    elif sorted_array[mid] > target:
        high = mid - 1
        return binary_search_two(sorted_array,low,high, target)
    else:
        low = mid + 1
        return binary_search_two(sorted_array, low, high, target)


if __name__ == "__main__":
    arrary = [x for x in range(10)]
    res = binary_search(arrary, 5)
    print(res)

    res = binary_search_two(arrary, 0, len(arrary) - 1, 5)
    print(res)
